using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Student> list = new List<Student>();

            Student s2 = new Student(1, "张三", 20);
            Student s3 = new Student(2, "li三", 18);
            Student s4 = new Student(4, "wu三", 30);
            Student s5 = new Student(5, "liu三", 61);
            Student s6 = new Student(3, "qi三", 10);
            Student s7 = new Student(7, "xiao三", 15);
            list.Add(s2);
            list.Add(s3);
            list.Add(s4);
            list.Add(s5);
            list.Add(s6);
            list.Add(s7);
            Console.WriteLine("请选择：1、添加学生信息。2、查询学生信息，3、删除学生信息。");
            string str = Console.ReadLine();
            int num = int.Parse(str);
            switch (num)
            {
                case 1:
                    Addstudent(list);
                    break;
                case 2:
                    Select(list);
                    break;
                case 3:
                    list.Clear();
                  
                    break;
                default:
                    break;
            }
        }

        private static void Select(List<Student> list)
        {
            Console.WriteLine("请输入要查询的功能：1、查询所有（按学号排序）2、查询所有（按姓名排序），3、查询所有（按年龄排序）4、按学号查询（查没有，则打印查无此学生）5、退出");
            int key = int.Parse(Console.ReadLine());

            switch (key) {
                case 1:
                    list.Sort();
                    ForE(list);
                    break;
                case 2:
                    list.Sort(new NameCom());
                    ForE(list);
                    break;
                case 3:
                    list.Sort(new AgeCom());
                    ForE(list);
                    break;
                case 4:
                    list.Sort();
                    ForE(list);
                    break;
                case 5:
                    Process.GetCurrentProcess().Kill();
                    break;
            }
        }

        private static void ForE(List<Student> list)
        {
            foreach (Student item in list)
            {
                Console.WriteLine(item);
            }
        }

        private static void Addstudent(List<Student> list)
        {
           
           
            a:  Console.WriteLine("请输入学号");
            int Num = int.Parse(Console.ReadLine());
            Console.WriteLine("请输入姓名：");
            string Name = Console.ReadLine();
            Console.WriteLine("请输入年龄");
            int Age = int.Parse(Console.ReadLine());
            Student student = new Student(Num, Name, Age);
            
          
            foreach (Student item in list)
            {
                if (item.Num == Num) {
                    Console.WriteLine("输入错误，请重试");
                    goto a;
                }
            }
            
            
            list.Add(student);




            foreach (Student item in list)
            {
                Console.WriteLine(item);
            }

        }
    }
    class Student : IComparable<Student>
    {
        private int num;
        private string name;
        private int age;
        public int Num { get { return num; } set { num = value; } }
        public string Name { get { return name; } set { name = value; } }
        public int Age { get { return age; } set { age = value; } }
        public Student() { }
        public Student(int num, string name, int age)
        {
            this.num = num;
            this.name = name;
            this.age = age;
        }
        public override string ToString()
        {
            return "学号：" + Num+" " + "姓名：" + Name + " " + "年龄:" + Age;
        }

        public int CompareTo(Student other)
        {
            return this.num.CompareTo(other.Num); 
        }
    }

    class NameCom : IComparer<Student>
    {
        public int Compare(Student x, Student y)
        {
            return x.Name.CompareTo(y.Name);
        }
    }



    class AgeCom : IComparer<Student>
    {
        public int Compare(Student x, Student y)
        {
            return x.Age.CompareTo(y.Age);
        }
    }
}
